<?php
$ro=file_get_contents('php://input'); //得到前端的json文件
$user=json_decode($ro,true);   //转变成php数组
$admin=$user['account'];   //取出对应数据
$password=$user['password'];
$flag=$user['flag'];

$link=new mysqli('127.0.0.1','root','root','books_manage_system','3306');
if($link->connect_error){
    die('连接失败'.$link->connect_error);
}else{
    //连接成功，设置编码格式
    mysqli_set_charset($link,'utf8');
    $link->query('SET NAMES UTF8');

    //存储数据
    $result=array();

    if($flag=='forget'){
        /* 忘记密码 */
        $sql="select * from administrator where account='$admin'";
        $sql1="select * from administrator where password='$password'";
        $sql2="update administrator set password='$password' where account='$admin'";
        $re=$link->query($sql);
        $num=mysqli_num_rows($re);
        if($num>0){
            $re1=$link->query($sql1);
            $num1=mysqli_num_rows($re1);
            if($num1>0){
                $result['status']='fail';
                $result['message']='新旧密码相同';
            }else{
                $re2=$link->query($sql2);
                if($re2>0){
                    $result['status']='success';
                    $result['message']='修改密码成功';
                }else{
                    $result['status']='fail';
                    $result['message']='修改密码失败';
                }
                mysqli_free_result($re1);
            }
        }else{
            $result['status']='fail';
            $result['message']='当前管理员不存在';
        }
        mysqli_free_result($re);
    }else{
        /* 注册 */
        $sql="select * from administrator where account='$admin'";
        $sql1="insert into administrator (id,account,password,state) values (NULL,'$admin','$password',0)";
        $re=$link->query($sql);
        $num=mysqli_num_rows($re);
        if($num>0){
            $result['status']='fail';
            $result['message']='当前管理员已存在';
        }else{
            $re1=$link->query($sql1);
            if($re1>0){
                $result['status']='success';
                $result['message']='注册成功';
            }else{
                $result['status']='fail';
                $result['message']='注册失败';
            }
        }
        mysqli_free_result($re);
    }
//关闭数据库
$link->close();
die(json_encode($result));
}

?>